<?php
/**
 * Created by PhpStorm.
 * User: a
 * Date: 2016/6/7
 * Time: 17:31
 */
namespace Admin\Controller\Report;
use Think\Controller;

class ApplyreportController extends Controller{

    /**
     * 贷款信息申请报文table
     */
    public function apply_report(){
        $start_time=I("post.start_time","");
        $end_time=I("post.end_time","");
        $borrow_status=I("post.borrow_status","");
        $report=D("Report");
        $report_array=$report->get_borrow_apply_info($start_time,$end_time,$borrow_status);
        $json=json_encode($report_array);
        $this->assign('report_json',$json);

        $borrow=D("Borrowinfo");
        $borrow_status_array=$borrow->borrow_status();
        $this->assign('borrow_status',$borrow_status_array);

        $search_array['start_time']=$start_time;
        $search_array['end_time']=$end_time;
        $search_array['borrow_status']=$borrow_status;
        $this->assign("search_array",$search_array);
        $this->display();
    }

    /**
     * 贷款信息申请报文导出
     */
    public function apply_report_export(){
        $start_time=I("post.start_time","");
        $end_time=I("post.end_time","");
        $borrow_status=I("post.borrow_status","");

        $report=D("Report");
        $report_array=$report->get_borrow_apply_info($start_time,$end_time,$borrow_status);
        $count=count($report_array);

        $info_type=C("apply_info_type");//信息类别
        $org_code=C("org_code");

        $number_array=C("number_array");
        $flowing_water="";
        for($j=0;$j<3;$j++){
            $k=rand(0,36);
            $flowing_water.=$number_array[$k];
        }
        $dir=$org_code.date("Ymd",time()).$flowing_water."1";
        $file_name=$dir.".txt";
        $path="Public/file/".$file_name;
        $file=fopen($path,"a+");

        $identity_type=0;//身份证类型：0代表身份证
        $apply_type=91;//贷款申请类型：91代表个人消费贷款
        for($i=0;$i<$count;$i++){
            $borrow_id=$report_array[$i]["borrow_id"];
            $cust_name=$report_array[$i]['cust_name'];
            if($cust_name==''){
                continue;
            }
            $cust_name=iconv("UTF-8","gbk//TRANSLIT",$cust_name);
            $identity_number=$report_array[$i]["cust_identity"];
            $borrow_money=$report_array[$i]['borrow_money'];
            $borrow_period=$report_array[$i]['borrow_loanperiod'];
            $borrow_createdate=$report_array[$i]['borrow_createdate'];
            $borrow_stateid=$report_array[$i]['borrow_stateid'];
            if($borrow_stateid==1||$borrow_stateid==7||$borrow_stateid==5){
                $borrow_stateid=1;
            }elseif($borrow_stateid==2){
                $borrow_stateid=2;
            }else{
                $borrow_stateid=3;
            }
            $file_string=$info_type.$org_code.str_pad($borrow_id,40," ",STR_PAD_RIGHT).str_pad($cust_name,30," ",STR_PAD_RIGHT).
            $identity_type. str_pad($identity_number,18," ",STR_PAD_RIGHT).$apply_type.str_pad($borrow_money,10,"0",STR_PAD_LEFT).
            str_pad($borrow_period,6," ",STR_PAD_RIGHT).$borrow_createdate.$borrow_stateid;

            fwrite($file,$file_string." \r\n");
        }
        $this->ajaxReturn($file_name);
    }
}